In [1]:
%load_ext autoreload
%autoreload 2
import os
import sys
# Get current Directory
wd = os.getcwd()
# Add package to path
#sys.path.append(os.path.join(os.path.abspath(os.path.join(wd, os.pardir)), 'openigtlink'))
sys.path.append(os.path.abspath(os.path.join(wd, os.pardir)))
In [4]:
from openigtlink import OpenIGTLink
import numpy
In [5]:
header = OpenIGTLink.OpenIGTLinkHeader()
header.setTYPE('STATUS')
header.setDEVICE_NAME('Device Name Example')
header.setTIME_STAMP(123,0.456)
bodyExample = 'This is an example of body message'
header.setCRC64(bodyExample) # Need the body to calculate the CRC
header.setBODY_SIZE(len(bodyExample))
print header
messageHeader = header.getHeaderMessage()
headerTest = OpenIGTLink.OpenIGTLinkHeader()
headerTest.unpack(messageHeader)
headerTest.setCRC64(bodyExample)
print headerTest
In [6]:
body = OpenIGTLink.OpenIGTLinkBody()
bodyExample = 'This is an example of body message'
body.setBodyMessage(bodyExample) # Need the body to calculate the CRC
print body
In [7]:
igtmessage = OpenIGTLink.OpenIGTLinkMessage()
igtmessage.header = OpenIGTLink.OpenIGTLinkHeader()
igtmessage.header.setTYPE('STATUS')
igtmessage.header.setDEVICE_NAME('Device Name Example')
igtmessage.header.setTIME_STAMP(123,0.456)
bodyExample = 'This is an example of body message'
igtmessage.body.setBodyMessage(bodyExample)
bodyCoding = '>' + 'c' * len(bodyExample)
igtmessage.__updateMessage__(bodyCoding)
print igtmessage
In [8]:
transform = OpenIGTLink.OpenIGTLinkTransform()
npTransform = numpy.eye(4)
npTransform[0,0] = 2
npTransform[1,1] = 3
npTransform[3,1] = 15
message = transform.setOpenIGTLinkTransform( npTransform = npTransform,\
floatTimeStamp = 123.456,\
transformName = 'RigidToTracker Test')
print transform
transformTest = OpenIGTLink.OpenIGTLinkTransform()
transformTest.header.unpack(message[0:transformTest.header.IGTLinkHeaderSize])
transformTest.unpackTransform(message[transformTest.header.IGTLinkHeaderSize::])
print transformTest
In [9]:
status = OpenIGTLink.OpenIGTLinkStatus()
message = status.setOpenIGTLinkStatus(statusCode = 9,\
statusSubCode = 254,\
errorName = 'Example Error Name',\
statusMessage = None,\
deviceName = 'Tracker',\
floatTimeStamp = 123.456)
print status
statusTest = OpenIGTLink.OpenIGTLinkStatus()
statusTest.header.unpack(message[0:transformTest.header.IGTLinkHeaderSize])
statusTest.unpackStatus(message[transformTest.header.IGTLinkHeaderSize::])
print statusTest
In [10]:
import struct
messagefile = os.path.join(os.path.join(os.path.join(os.path.abspath(os.path.join(wd, os.pardir)),\
'OpenIGTLink'),\
'ExampleFiles'),\
'ExampleMessages.txt')
text_file = open(messagefile, "r")
messages = text_file.read()
text_file.close()
firstMessage = messages[0:106]
a = repr(firstMessage)
transform = OpenIGTLink.OpenIGTLinkTransform()
transform.header.unpack(firstMessage[0:transform.header.IGTLinkHeaderSize])
transform.unpackTransform(firstMessage[transform.header.IGTLinkHeaderSize::])
print 'Byte Representation equal: ', repr(transform.getMessageToSend()) == repr(firstMessage)
In [11]:
import pandas
messagefile = os.path.join(os.path.join(os.path.join(os.path.abspath(os.path.join(wd, os.pardir)),\
'OpenIGTLink'),\
'ExampleFiles'),\
'Model.csv')
df = pandas.read_csv(messagefile)
df
Out[11]:
In [12]:
messageList = list()
for i in range(len(df.index)):
message = df.loc[i]
m = None
if message['TYPE'] == 'TRANSFORM':
npTransform = numpy.eye(4)
for ii in range(3):
for jj in range(4):
npTransform[ii,jj] = float(message['T' + str(ii) + str(jj)])
t = OpenIGTLink.OpenIGTLinkTransform()
m = t.setOpenIGTLinkTransform(npTransform = npTransform,\
floatTimeStamp = float(message['TIME_STAMP']),\
transformName = message['DEVICE_NAME'])
messageList.append(m)
elif message['TYPE'] == 'STATUS':
s = OpenIGTLink.OpenIGTLinkStatus()
m = s.setOpenIGTLinkStatus(statusCode = int(message['CODE']),\
statusSubCode = int(message['SUBCODE']),\
errorName = message['ERROR_NAME'],\
statusMessage = message['STATUS_MESSAGE'],\
deviceName = message['DEVICE_NAME'],\
floatTimeStamp = float(message['TIME_STAMP']))
messageList.append(m)
else:
print "Problem realind row: ", i
for i, m in enumerate(messageList):
print '[{0:d}]'.format(i), m
In [ ]:
%load_ext autoreload
%autoreload 2
import os
import sys
import time
# Get current Directory
wd = os.getcwd()
# Add package to path
sys.path.append(os.path.join(os.path.abspath(os.path.join(wd, os.pardir)), 'OpenIGTLink'))
import OpenIGTLink
messagefile = os.path.join(os.path.join(os.path.join(os.path.abspath(os.path.join(wd, os.pardir)),\
'OpenIGTLink'),\
'ExampleFiles'),\
'Model.csv')
server = OpenIGTLink.OpenIGTLinkServer()
server.addMessageFile(messagefile, 'File #1')
server.connect()
time.sleep(3)
server.sendData(rate = 1.0)
server.disconnect()
In [ ]:
%load_ext autoreload
%autoreload 2
import os
import sys
import time
# Get current Directory
wd = os.getcwd()
# Add package to path
sys.path.append(os.path.join(os.path.abspath(os.path.join(wd, os.pardir)), 'OpenIGTLink'))
import OpenIGTLink
messagefileReceived = os.path.join(os.path.join(os.path.join(os.path.abspath(os.path.join(wd, os.pardir)),\
'OpenIGTLink'),\
'ExampleFiles'),\
'ModelReceived.csv')
client = OpenIGTLink.OpenIGTLinkClient()
client.connect()
time.sleep(2)
client.listenData()
time.sleep(5)
client.disconnect()
client.writeFile(messagefileReceived)